# 使用卷积神经网络进行图像分类
from keras.backend import flatten
from keras.layers.core import Activation, Dense,activations
from keras.models import Sequential
from keras.utils.vis_utils import plot_model
from keras.layers import Flatten
from keras.layers.core import Dense
from keras.datasets import mnist
import keras 
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import Reshape
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
import tensorflow

num_classes = 10
batch_size = 32
epochs = 10 
img_row, img_col = 28, 28
(x_train,y_train),(x_test,y_test) = mnist.load_data()
y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)

input_shape = (28,28)
inputs = Input(input_shape)
print(input_shape,(1,))
x = Reshape(input_shape+(1,),input_shape=input_shape)(inputs)
conv1 = Conv2D(14,kernel_size=4, activation='relu')(x)
pool1 = MaxPooling2D(pool_size=(2,2),)(conv1)
conv2 = Conv2D(7,kernel_size=4,activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2,2))(conv2)
flatten = Flatten()(pool2)
outputs = Dense(10,activation='sigmoid')(flatten)
model = Model(inputs = inputs,outputs=outputs)
print(model.summary())

plot_model(model,to_file='conv.png')
opt = keras.optimizers.RMSprop(learning_rate=0.0001,decay = 1e-6)
model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,validation_data=(x_test,y_test),shuffle=True)
scores = model.evaluate(x_test,y_test,verbose=1)
print("Test loss:",scores[0])
print("Test accuracy:",scores[1])



